﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace ADayRoiAdmin
{
    public partial class Menu : Form
    {
        static SqlConnection con = null;
        AdayroiClass adayroi = new AdayroiClass(ref con);
        
        public Menu()
        {
            InitializeComponent();
        }

        private void btnAddCap1_Click(object sender, EventArgs e)
        {
            string sql = "insert into Category (Name,Parent,IsDisplay,Sort,URL) values(N'" + txtNameCap1.Text +
                "' ,0, '" + chk_HienThiCap1.Checked + "' ," + txtDoUuTienCap1.Text + ", '" + txtLink1.Text + "')";
            if(adayroi.UpdateSQL(con,sql)==0)
                MessageBox.Show("Thành Công");
            list_cap1.DataSource = getListMenu(0);
            list_cap1.ValueMember = "PKID";
            list_cap1.DisplayMember = "Name";   

        }
      

        private void Menu_Load(object sender, EventArgs e)
        {
            list_cap1.DataSource = getListMenu(0);
            list_cap1.ValueMember = "PKID";
            list_cap1.DisplayMember = "Name";           

        }

        private DataTable getListMenu(int parent)
        {
            string sql = "select * from Category where Parent = " + parent + " order by Sort";
            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            try
            {
                con.Open();
                adapter.Fill(ds);
                dt = ds.Tables[0];
                con.Close();
              
            }
            catch (SqlException ex)
            {
                MessageBox.Show("Lỗi: " + ex.ToString());
            }
            return dt;
        }

        private void list_cap1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int menu1ID = Convert.ToInt32(list_cap1.SelectedValue);
                list_cap2.DataSource = getListMenu(menu1ID);
                list_cap2.ValueMember = "PKID";
                list_cap2.DisplayMember = "Name";

                DataTable dt_master = getListMenu(0);
                DataRow dr_details = dt_master.Select(" PKID = " + menu1ID).FirstOrDefault();
                txtNameCap1.Text = dr_details["Name"].ToString();
                chk_HienThiCap1.Checked = Convert.ToBoolean(dr_details["IsDisplay"]);
                txtDoUuTienCap1.Text = dr_details["Sort"].ToString();
                txtLink1.Text = dr_details["URL"].ToString();
            }
            catch { }
        }

        private void list_cap2_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int menu2ID = Convert.ToInt32(list_cap2.SelectedValue);
                list_cap3.DataSource = getListMenu(menu2ID);
                list_cap3.ValueMember = "PKID";
                list_cap3.DisplayMember = "Name";

                DataTable dt_master = getListMenu(Convert.ToInt32(list_cap1.SelectedValue));
                DataRow dr_details = dt_master.Select(" PKID = " + menu2ID).FirstOrDefault();
                txtNameCap2.Text = dr_details["Name"].ToString();
                chkHienThiCap2.Checked = Convert.ToBoolean(dr_details["IsDisplay"]);
                txtDoUuTienCap2.Text = dr_details["Sort"].ToString();
            }
            catch { }
        }

        private void list_cap3_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int menu3ID = Convert.ToInt32(list_cap3.SelectedValue);      
                DataTable dt_master = getListMenu(Convert.ToInt32(list_cap2.SelectedValue));
                DataRow dr_details = dt_master.Select(" PKID = " + menu3ID).FirstOrDefault();
                txtNameCap3.Text = dr_details["Name"].ToString();
                chkHienThiCap3.Checked = Convert.ToBoolean(dr_details["IsDisplay"]);
                txtDoUuTienCap3.Text = dr_details["Sort"].ToString();
            }
            catch { }
        }

        private void btnEditCap1_Click(object sender, EventArgs e)
        {
            string sql = "update Category set Name= N'" +
                txtNameCap1.Text + "',IsDisplay= '" + chk_HienThiCap1.Checked + "', URL= '" + txtLink1.Text 
                + "', Sort = " + txtDoUuTienCap1.Text +
                " where PKID = " + Convert.ToInt32(list_cap1.SelectedValue);
            if (adayroi.UpdateSQL(con, sql) == 0)
                MessageBox.Show("Thành Công");
            list_cap1.DataSource = getListMenu(0);
            list_cap1.ValueMember = "PKID";
            list_cap1.DisplayMember = "Name"; 
        }

        private void btnAddCap2_Click(object sender, EventArgs e)
        {
            int parentid = Convert.ToInt32(list_cap1.SelectedValue);
            string sql = "insert into Category (Name,Parent,IsDisplay,Sort) values(N'" + txtNameCap2.Text +
               "' ," + parentid + ", '" + chkHienThiCap2.Checked + "' ," + txtDoUuTienCap2.Text + ")";
            if (adayroi.UpdateSQL(con, sql) == 0)
                MessageBox.Show("Thành Công");
            list_cap2.DataSource = getListMenu(parentid);
            list_cap2.ValueMember = "PKID";
            list_cap2.DisplayMember = "Name";
        }

        private void btnEditCap2_Click(object sender, EventArgs e)
        {
            string sql = "update Category set Name= N'" +
                txtNameCap2.Text + "',IsDisplay= '" + chkHienThiCap2.Checked + "', Sort=" + txtDoUuTienCap2.Text +
                "where PKID = " + Convert.ToInt32(list_cap2.SelectedValue);
            if (adayroi.UpdateSQL(con, sql) == 0)
                MessageBox.Show("Thành Công");
            list_cap2.DataSource = getListMenu(Convert.ToInt32(list_cap1.SelectedValue));
            list_cap2.ValueMember = "PKID";
            list_cap2.DisplayMember = "Name"; 
        }

        private void btnAddCap3_Click(object sender, EventArgs e)
        {
            int parentid = Convert.ToInt32(list_cap2.SelectedValue);
            string sql = "insert into Category (Name,Parent,IsDisplay,Sort) values(N'" + txtNameCap3.Text +
               "' ," + parentid + ", '" + chkHienThiCap3.Checked + "' ," + txtDoUuTienCap3.Text + ")";
            if (adayroi.UpdateSQL(con, sql) == 0)
                MessageBox.Show("Thành Công");
            list_cap3.DataSource = getListMenu(parentid);
            list_cap3.ValueMember = "PKID";
            list_cap3.DisplayMember = "Name";
        }

        private void btnEditCap3_Click(object sender, EventArgs e)
        {
            string sql = "update Category set Name= N'" +
                txtNameCap3.Text + "',IsDisplay= '" + chkHienThiCap3.Checked + "', Sort=" + txtDoUuTienCap3.Text +
                "where PKID = " + Convert.ToInt32(list_cap3.SelectedValue);
            if (adayroi.UpdateSQL(con, sql) == 0)
                MessageBox.Show("Thành Công");
            list_cap3.DataSource = getListMenu(Convert.ToInt32(list_cap2.SelectedValue));
            list_cap3.ValueMember = "PKID";
            list_cap3.DisplayMember = "Name"; 
        }
        private void btnDelCap3_Click(object sender, EventArgs e)
        {
            DialogResult resul = MessageBox.Show("Bạn có chắc chắn muốn xóa???", "Chú Ý", MessageBoxButtons.YesNo, MessageBoxIcon.Stop);
            if (resul == DialogResult.Yes)
            {
                string sql = "delete from Category where PKID = " + Convert.ToInt32(list_cap3.SelectedValue);
                if (adayroi.UpdateSQL(con, sql) == 0)
                    MessageBox.Show("Thành Công");
                list_cap3.DataSource = getListMenu(Convert.ToInt32(list_cap2.SelectedValue));
                list_cap3.ValueMember = "PKID";
                list_cap3.DisplayMember = "Name";
            }
        }

        private void btnDelCap2_Click(object sender, EventArgs e)
        {
            DialogResult resul = MessageBox.Show("Bạn có chắc chắn muốn xóa???", "Chú Ý", MessageBoxButtons.YesNo, MessageBoxIcon.Stop);
            if (resul == DialogResult.Yes)
            {
                string sql = "delete from Category where PKID = " + Convert.ToInt32(list_cap2.SelectedValue);
                if (adayroi.UpdateSQL(con, sql) == 0)
                    MessageBox.Show("Thành Công");
                list_cap2.DataSource = getListMenu(Convert.ToInt32(list_cap1.SelectedValue));
                list_cap2.ValueMember = "PKID";
                list_cap2.DisplayMember = "Name";
            }
        }

        private void btnDelCap1_Click(object sender, EventArgs e)
        {
            DialogResult resul = MessageBox.Show("Bạn có chắc chắn muốn xóa???", "Chú Ý", MessageBoxButtons.YesNo, MessageBoxIcon.Stop);
            if (resul == DialogResult.Yes)
            {
                string sql = "delete from Category where PKID = " + Convert.ToInt32(list_cap1.SelectedValue);
                if (adayroi.UpdateSQL(con, sql) == 0)
                    MessageBox.Show("Thành Công");
                list_cap1.DataSource = getListMenu(0);
                list_cap1.ValueMember = "PKID";
                list_cap1.DisplayMember = "Name";
            }
        }
    }
}
